export function hex2int(hex: string): number {
    const len = hex.length;
    const a = new Array(len);
    let code;
    for (let i = 0; i < len; i++) {
        code = hex.charCodeAt(i);
        if (48 <= code && code < 58) {
            code -= 48;
        } else {
            code = (code & 0xdf) - 65 + 10;
        }
        a[i] = code;
    }

    return a.reduce(function (acc, c) {
        acc = 16 * acc + c;
        return acc;
    }, 0);
}

export function int2hex(num: number): string {
    if (num <= 0) {
        return "0000";
    }
    const hex = "0123456789ABCDEF";
    let s = "";
    while (num) {
        s = hex.charAt(num % 16) + s;
        num = Math.floor(num / 16);
    }
    if (s.length < 4) {
        for (let i = 0; i <= 4 - s.length; i++) {
            s = "0" + s;
        }
    }
    //这有个 bug ，必须走两遍
    if (s.length < 4) {
        for (let i = 0; i <= 4 - s.length; i++) {
            s = "0" + s;
        }
    }
    return s;
}